{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# docker"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 構築\n",
    "\n",
    "https://github.com/ftakao2007/work\n",
    "\n",
    "### 階層構造\n",
    "\n",
    "* work\n",
    "    * build\n",
    "        * dockerhost : dockerhostを構築する(ubuntu,centos7)\n",
    "            * コマンド例) ansible-playbook -i \"127.0.0.1,\" -k -K -c paramiko centos7.yml\n",
    "        * container\n",
    "            * base : OS設定等基礎的なコンテナを作成する\n",
    "                * コマンド例) sh create_container_centos7_base.sh\n",
    "            * mysql : mysqlをコンテナにインストールする\n",
    "                * コマンド例) sh create_container_mysql5.6_centos7.sh\n",
    "            * (etc...)\n",
    "        * docker-compose : docker-composeファイル置き場\n",
    "\n",
    "\n",
    "1. work/build/dockerhostでdockerホストを構築 (初回のみ)\n",
    "1. work/build/container/baseでOSの基本部分を構築する (初回のみ)\n",
    "1. work/build/container/(各種サービス)でサービスのコンテナを構築し、dockerhubにpushする\n",
    "1. work/build/docker-compose に先ほどdockerhubにpushしたコンテナを使うためのdocker-composeファイルを作成する"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## mysqlをインストールする例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## mysql-clientインストール\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### コンテナの作成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "base_centos7\n",
      "base_centos7\n",
      "\n",
      "PLAY [containers] **************************************************************\n",
      "\n",
      "TASK [mysql5.6.centos7 : MySQL Communityのrpmのインストール(MySQL5.6をインストールするため)] ******\n",
      "changed: [base_centos7]\n",
      "\n",
      "TASK [mysql5.6.centos7 : 必要なパッケージのインストール] **************************************\n",
      "changed: [base_centos7] => (item=[u'mysql-community-client.x86_64', u'mysql-community-devel.x86_64', u'mysql-community-server.x86_64'])\n",
      "\n",
      "TASK [mysql5.6.centos7 : my.cnfの設置] ********************************************\n",
      "changed: [base_centos7]\n",
      "\n",
      "TASK [mysql5.6.centos7 : MySQL データディレクトリを初期化し、システムテーブルを作成] *********************\n",
      "changed: [base_centos7]\n",
      "\n",
      "TASK [mysql5.6.centos7 : mysqlの起動と自動起動設定] **************************************\n",
      "changed: [base_centos7]\n",
      "\n",
      "TASK [mysql5.6.centos7 : rootユーザのパスワード変更] **************************************\n",
      "changed: [base_centos7]\n",
      "\n",
      "TASK [mysql5.6.centos7 : mysqlユーザの作成] ******************************************\n",
      "changed: [base_centos7] => (item=127.0.0.0/255.255.255.0)\n",
      "changed: [base_centos7] => (item=localhost)\n",
      "changed: [base_centos7] => (item=%)\n",
      "\n",
      "TASK [mysql5.6.centos7 : データベースの作成] ********************************************\n",
      "changed: [base_centos7]\n",
      "\n",
      "PLAY RECAP *********************************************************************\n",
      "base_centos7               : ok=8    changed=8    unreachable=0    failed=0   \n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Creating base_centos7\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### ansibleを使うためにpython2系の環境を設定\n",
    "export PYENV_VERSION=\"work\"\n",
    "#pyenv versions\n",
    "\n",
    "work_dir=\"/home/jsd/work/build/container\"\n",
    "cd ${work_dir}/mysql\n",
    "sh create_container_mysql5.6_centos7.sh"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### コンテナをdockerhubにpush\n",
    "\n",
    "```\n",
    "### ログイン\n",
    "$ docker login\n",
    "================================================\n",
    "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.\n",
    "Username (ftakao2007):\n",
    "Password:\n",
    "Login Succeeded\n",
    "================================================\n",
    "\n",
    "### コンテナを停止\n",
    "$ docker stop base_centos7\n",
    "================================================\n",
    "base_centos7\n",
    "================================================\n",
    "\n",
    "### コミット\n",
    "$ docker commit base_centos7 ftakao2007/mysql5.6_centos7:latest\n",
    "================================================\n",
    "sha256:b8b3619cf77682a92ec264528648d69a08c93a021ae9ff65cb8df34eeccfcec3\n",
    "================================================\n",
    "\n",
    "### dockerhubにpush\n",
    "$ docker push ftakao2007/mysql5.6_centos7:latest\n",
    "================================================\n",
    "The push refers to a repository [docker.io/ftakao2007/mysql5.6_centos7]\n",
    "775911eff0d1: Pushed\n",
    "c98c19d806c4: Mounted from ftakao2007/centos7\n",
    "99b28d9413e4: Mounted from ftakao2007/centos7\n",
    "latest: digest: sha256:fd2a322cdbafb5d035b16855f64f46a1768ca1b8e029276f43d98759bde40455 size: 955\n",
    "================================================\n",
    "\n",
    "### ブラウザでアクセスして確認\n",
    "https://hub.docker.com/r/ftakao2007/mysql5.6_centos7/\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### docker-composeファイルの作成\n",
    "\n",
    "```\n",
    "### ディレクトリに移動\n",
    "$ cd ../docker-compose\n",
    "\n",
    "### docker-composeファイルの作成\n",
    "$ vi docker-compose_mysql5.6_centos7.yml\n",
    "================================================\n",
    "mysql5.6_centos7:\n",
    "   image: ftakao2007/mysql5.6_centos7:latest\n",
    "   container_name: mysql5.6_centos7\n",
    "   privileged: true\n",
    "   tty: true\n",
    "   ports:\n",
    "     - \"3306:3306\"\n",
    "     - \"2222:22\"\n",
    "   command: sh -c 'exec /usr/sbin/init && systemctl restart sshd.service && systemctl restart mysqld.service'\n",
    "================================================\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded plugins: fastestmirror\n",
      "Examining /var/tmp/yum-root-vps5IL/mysql-community-release-el7-5.noarch.rpm: mysql-community-release-el7-5.noarch\n",
      "Marking /var/tmp/yum-root-vps5IL/mysql-community-release-el7-5.noarch.rpm to be installed\n",
      "Resolving Dependencies\n",
      "--> Running transaction check\n",
      "---> Package mysql-community-release.noarch 0:el7-5 will be installed\n",
      "--> Finished Dependency Resolution\n",
      "\n",
      "Dependencies Resolved\n",
      "\n",
      "================================================================================\n",
      " Package               Arch   Version\n",
      "                                    Repository                             Size\n",
      "================================================================================\n",
      "Installing:\n",
      " mysql-community-release\n",
      "                       noarch el7-5 /mysql-community-release-el7-5.noarch 4.3 k\n",
      "\n",
      "Transaction Summary\n",
      "================================================================================\n",
      "Install  1 Package\n",
      "\n",
      "Total size: 4.3 k\n",
      "Installed size: 4.3 k\n",
      "Downloading packages:\n",
      "Running transaction check\n",
      "Running transaction test\n",
      "Transaction test succeeded\n",
      "Running transaction\n",
      "  Installing : mysql-community-release-el7-5.noarch                         1/1 \n",
      "  Verifying  : mysql-community-release-el7-5.noarch                         1/1 \n",
      "\n",
      "Installed:\n",
      "  mysql-community-release.noarch 0:el7-5                                        \n",
      "\n",
      "Complete!\n",
      "Loaded plugins: fastestmirror\n",
      "Loading mirror speeds from cached hostfile\n",
      " * base: ftp.jaist.ac.jp\n",
      " * extras: mirror.nus.edu.sg\n",
      " * updates: mirror.nus.edu.sg\n",
      "Resolving Dependencies\n",
      "--> Running transaction check\n",
      "---> Package mysql-community-client.x86_64 0:5.6.36-2.el7 will be installed\n",
      "--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.36-2.el7.x86_64\n",
      "--> Running transaction check\n",
      "---> Package mariadb-libs.x86_64 1:5.5.52-1.el7 will be obsoleted\n",
      "---> Package mysql-community-libs.x86_64 0:5.6.36-2.el7 will be obsoleting\n",
      "--> Processing Dependency: mysql-community-common(x86-64) >= 5.6.10 for package: mysql-community-libs-5.6.36-2.el7.x86_64\n",
      "--> Running transaction check\n",
      "---> Package mysql-community-common.x86_64 0:5.6.36-2.el7 will be installed\n",
      "--> Finished Dependency Resolution\n",
      "\n",
      "Dependencies Resolved\n",
      "\n",
      "================================================================================\n",
      " Package                   Arch      Version         Repository            Size\n",
      "================================================================================\n",
      "Installing:\n",
      " mysql-community-client    x86_64    5.6.36-2.el7    mysql56-community     19 M\n",
      " mysql-community-libs      x86_64    5.6.36-2.el7    mysql56-community    2.0 M\n",
      "     replacing  mariadb-libs.x86_64 1:5.5.52-1.el7\n",
      "Installing for dependencies:\n",
      " mysql-community-common    x86_64    5.6.36-2.el7    mysql56-community    257 k\n",
      "\n",
      "Transaction Summary\n",
      "================================================================================\n",
      "Install  2 Packages (+1 Dependent package)\n",
      "\n",
      "Total download size: 22 M\n",
      "Downloading packages:\n",
      "Public key for mysql-community-common-5.6.36-2.el7.x86_64.rpm is not installed\n",
      "--------------------------------------------------------------------------------\n",
      "Total                                               15 MB/s |  22 MB  00:01     \n",
      "Retrieving key from file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql\n",
      "Running transaction check\n",
      "Running transaction test\n",
      "Transaction test succeeded\n",
      "Running transaction\n",
      "  Installing : mysql-community-common-5.6.36-2.el7.x86_64                   1/4 \n",
      "  Installing : mysql-community-libs-5.6.36-2.el7.x86_64                     2/4 \n",
      "  Installing : mysql-community-client-5.6.36-2.el7.x86_64                   3/4 \n",
      "  Erasing    : 1:mariadb-libs-5.5.52-1.el7.x86_64                           4/4 \n",
      "  Verifying  : mysql-community-common-5.6.36-2.el7.x86_64                   1/4 \n",
      "  Verifying  : mysql-community-libs-5.6.36-2.el7.x86_64                     2/4 \n",
      "  Verifying  : mysql-community-client-5.6.36-2.el7.x86_64                   3/4 \n",
      "  Verifying  : 1:mariadb-libs-5.5.52-1.el7.x86_64                           4/4 \n",
      "\n",
      "Installed:\n",
      "  mysql-community-client.x86_64 0:5.6.36-2.el7                                  \n",
      "  mysql-community-libs.x86_64 0:5.6.36-2.el7                                    \n",
      "\n",
      "Dependency Installed:\n",
      "  mysql-community-common.x86_64 0:5.6.36-2.el7                                  \n",
      "\n",
      "Replaced:\n",
      "  mariadb-libs.x86_64 1:5.5.52-1.el7                                            \n",
      "\n",
      "Complete!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "warning: /var/cache/yum/x86_64/7/mysql56-community/packages/mysql-community-common-5.6.36-2.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY\n",
      "Importing GPG key 0x5072E1F5:\n",
      " Userid     : \"MySQL Release Engineering <mysql-build@oss.oracle.com>\"\n",
      " Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5\n",
      " Package    : mysql-community-release-el7-5.noarch (installed)\n",
      " From       : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### mysqlクライアントのインストール\n",
    "sudo yum install -y http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm\n",
    "sudo yum install -y mysql-community-client.x86_64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE\n",
      "ftakao2007/mysql5.6_centos7   latest              09a773ad8e34        19 hours ago        2.16GB\n",
      "CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                                          NAMES\n",
      "d7e188a6bdfc        ftakao2007/mysql5.6_centos7:latest   \"sh -c 'exec /usr/...\"   4 minutes ago       Up 4 minutes        0.0.0.0:3306->3306/tcp, 0.0.0.0:2222->22/tcp   mysql5.6_centos7\n",
      "REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE\n",
      "ftakao2007/mysql5.6_centos7   latest              09a773ad8e34        19 hours ago        2.16GB\n",
      "CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                                          NAMES\n",
      "d7e188a6bdfc        ftakao2007/mysql5.6_centos7:latest   \"sh -c 'exec /usr/...\"   5 minutes ago       Up 5 minutes        0.0.0.0:3306->3306/tcp, 0.0.0.0:2222->22/tcp   mysql5.6_centos7\n",
      "CURDATE()\n",
      "2017-07-15\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "mysql5.6_centos7 is up-to-date\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "\n",
    "### イメージ、コンテナが無い状態の確認\n",
    "docker images\n",
    "docker ps -a\n",
    "\n",
    "### docker-composeディレクトリに移動\n",
    "#work_dir=\"/home/jsd/work/build/docker-compose\"\n",
    "work_dir=\"${PYENV_DIR}/work/build/docker-compose\"\n",
    "cd ${work_dir}\n",
    "\n",
    "### コンテナの作成\n",
    "docker-compose -f docker-compose_mysql5.6_centos7.yml up -d\n",
    "sleep 60\n",
    "\n",
    "### イメージ、コンテナの確認\n",
    "docker images\n",
    "docker ps -a\n",
    "\n",
    "### mysql接続\n",
    "mysql -ujoruri -p'pass' -h 127.0.0.1 -P 3306 -e \"SELECT CURDATE();\" 2> /dev/null"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
